<script>
  import {
    scanDeleteShoppingCart,
    scanAddShoppingCart,
    scanUpdateShoppingCart,
    scanQueryShoppingCart,
    scanQueryShoppingCartForCart
  } from "@/utils/ReturnAxios"
  import updataCart from "./index.vue"
  import {Toast} from '@/utils/toast.js'

  export default {
    mixins: [updataCart],
    data() {
      return {}
    },
    components: {},
    methods: {
      async ajaxAddCart() {
        var res = await
          scanAddShoppingCart(this.addConfig)
        res = res.data
        if (res.returnCode == 0) {
          Toast({
            message: res.msg,
            type: 'fail'
          })
          return
        }
        this.queryCartVuex(res.data)
      },


      queryCartVuex(data) {
        var activeCartList = data[0].shoppingCartLists
        this.$store.commit('cjy/SET_OFF_LINE_ACTIVE_DATA', activeCartList)
      },
      async ajaxupdataCart() {
        var res = await
          scanUpdateShoppingCart(this.upDateConfig)
        res = res.data
        if (res.returnCode == 0) {
          Toast({
            message: res.msg,
            type: 'fail'
          })
          return
        }

        this.upadatCartVues(res.data)
      },

      upadatCartVues(data) {
        var activeCartList = this.cartList
        data.map((newData) => {
          var goodsCode = newData.goodsCode
          var oldDataIndex = activeCartList.findIndex(
            (item) => {
              return item.goodsCode == goodsCode
            }
          )
          if (oldDataIndex !== -1) {
            var oldData = activeCartList[oldDataIndex]
            oldData.goodsNum = newData.goodsNum || oldData.goodsNum
            oldData.serve = newData.serve
            oldData.preorder = newData.preorder
            activeCartList.splice(oldDataIndex, 1, oldData)
          } else {
            activeCartList.push(newData)
          }
        })
        this.$store.commit('cjy/SET_OFF_LINE_ACTIVE_DATA', activeCartList)

      },

      async ajaxDeleFromCart(config) {
        if (!config) {
          var config = {
            shoppingCartList: [
              {rid: this.rid}
            ]
          }
        }
        var ids = config.shoppingCartList.map((item) => {
          return item.rid
        })
        ids = ids.join(',')
        var res = await scanDeleteShoppingCart(ids)
        res = res.data
        var shoppingCartList = config.shoppingCartList
        this.deleCartVuex(shoppingCartList)
      },

      deleCartVuex(data) {
        var cartList = JSON.parse(JSON.stringify(this.cartList))
        data.map((item) => {
            for (var i = 0; cartList.length; i++) {
              if (cartList[i].rid == item.rid) {
                cartList.splice(i, 1)
                break
              }
            }
          }
        )
        this.$store.commit('cjy/SET_OFF_LINE_ACTIVE_DATA', cartList)
      }
    },
    computed: {
      cartList() {
        return this.$store.getters['cjy/offLineAcitveData']
      },
      addConfig() {
        var workingInfo = this.workingInfo
        var storeCode = this.storeCode
        var goodsUnit = this.goodsUnit
        var data = this.commodityInfor
        var goodsNum = this.number
        var config = [{
          goodsCode: data.goodsCode,
          storeCode: this.storeCode || data.storeCode,
          goodsNum: goodsNum,
          goodsType: this.goodsType,
          workingInfo,
//          barCode:this.barCode
        }]
        return config
      },
    },
    created() {
    }
  }
</script>

<style>

</style>
